/**********************************************************************
 *
 * $RCSfile: MenuAccessEntityTemplateViewModel.java,v $  $Revision: 1.5 $  $Date: 2010/08/03 14:03:59 $
 *
 * $Log: MenuAccessEntityTemplateViewModel.java,v $
 * Revision 1.5  2010/08/03 14:03:59  @author simplefan
 * *** empty log message ***
 *
 * Revision 1.4  2009/06/16 09:13:48  @author simplefan
 * *** empty log message ***
 *
 * Revision 1.3  2009/06/08 12:14:04  @author simplefan
 * *** empty log message ***
 *
 * Revision 1.2  2009/06/08 09:41:28  @author simplefan
 * *** empty log message ***
 *
 * Revision 1.1  2009/03/27 06:34:06  @author simplefan
 * *** empty log message ***
 *
 *
 *
 *********************************************************************/
/*
 * Copyright 2008 gxlu, Inc. All rights reserved.
 * File name : MenuAccessEntityTemplateViewModel.java
 * Created on : Mar 26, 2009 11:35:44 AM
 * Creator : lethe
 */
package com.simplefan.meta.determine.client.web.model;

import java.util.ArrayList;
import java.util.List;

import com.bstek.dorado.data.Dataset;
import com.bstek.dorado.data.ParameterSet;
import com.bstek.dorado.data.Record;
import com.bstek.dorado.data.RecordIterator;
import com.bstek.dorado.view.ViewModel;
import com.bstek.dorado.view.data.ViewDataset;
import com.simplefan.framework.common.util.CommonUtil;
import com.simplefan.meta.determine.client.desktop.service.MetaPlatformServiceFacade;
import com.simplefan.meta.determine.common.po.EntityMgtTemplate;
import com.simplefan.meta.determine.common.po.MenuAccessEntityTemplate;
import com.simplefan.meta.determine.common.po.MenuItem;
import com.simplefan.system.server.consts.SystemLanguageConst;

/**
 * <pre>
 * Description : TODO
 * @author simplefan
 * </pre>
 */
public class MenuAccessEntityTemplateViewModel extends MetaPlatformViewModel {
	MetaPlatformServiceFacade facade = getServiceFacade();

	@Override
	protected void doLoadData(ViewDataset dataset) throws Exception {
		ParameterSet properties = dataset.parameters();
		if (dataset.getId().equals("datasetMenuItem")) {
			String parentItemId = properties.getString("parentItemId");
			if (parentItemId == null || parentItemId.length() == 0) {
				MenuItem item = new MenuItem();
				item.setDescription(SystemLanguageConst.getLanguageString ("metaPlatform", "main_menu"));
				item.setId(-1);
				dataset.fromDO(item);
			} else {
				List<MenuItem> list = facade.getServiceFactory().getMetaPlatformService().findMenuItemByParentId(
						Long.parseLong(parentItemId));

				dataset.fromDO(list);
			}
		}else if (dataset.getId().equals("datasetEntityTemplateLink")) {
			String menuItemId = properties.getString("menuItemId");
			if (menuItemId != null && !menuItemId.equals("-1")) {
				List<MenuAccessEntityTemplate> list =
				facade.getServiceFactory().getMetaPlatformService().findMenuAccessEntityTemplateByMenuId(Long.parseLong(menuItemId));
				List<EntityMgtTemplate> ret = new ArrayList<EntityMgtTemplate>();
				for (MenuAccessEntityTemplate menuTemplate : list) {
					menuTemplate.getEntityMgtTemplate().setViewModel(menuTemplate.getViewModel());
					ret.add(menuTemplate.getEntityMgtTemplate());
				}
				dataset.fromDO(ret);
			}
		}else if (dataset.getId().equals("datasetEntityTemplateNoLink")) {
			if(ViewModel.STATE_VIEW != dataset.getViewModel().getState()){
				String menuItemId = properties.getString("menuItemId");
				long entityId = properties.getLong("entityId");
				String alias = properties.getString("alias");
				String name = properties.getString("name");
				String code = properties.getString("code");
				List<EntityMgtTemplate> entityMgtTemplate = 
					facade.getServiceFactory().getMetaPlatformService().findEntityMgtTemplate(entityId, name, code, alias);
				if (entityMgtTemplate!=null && entityMgtTemplate.size()>0 &&
						menuItemId != null && !menuItemId.equals("-1")) {
					List<MenuAccessEntityTemplate> list =
					facade.getServiceFactory().getMetaPlatformService().findMenuAccessEntityTemplateByMenuId(Long.parseLong(menuItemId));
					for (MenuAccessEntityTemplate menuTemplate : list) {
						for (int i = 0; i < entityMgtTemplate.size(); i++) {
							if(entityMgtTemplate.get(i).getId() == menuTemplate.getId()){
								entityMgtTemplate.remove(i);
								break;
							}
						}
					}
				}
				dataset.fromDO(entityMgtTemplate);
			}
		}
		super.doLoadData(dataset);
	}

	@Override
	protected void doUpdateData(ParameterSet arg0, ParameterSet arg1) throws Exception {
		Dataset datasetMenu = getDataset("datasetMenuItem");
		MenuItem menuItem = (MenuItem) datasetMenu.toSingleDO();
		Dataset datasetLink = getDataset("datasetEntityTemplateLink");
		RecordIterator ri = datasetLink.recordIterator();
		List<MenuAccessEntityTemplate> list = new ArrayList<MenuAccessEntityTemplate>();
		while (ri.hasNext()) {
			Record record = ri.nextRecord();
			EntityMgtTemplate entity = (EntityMgtTemplate) datasetLink.toSingleDO(record);
			MenuAccessEntityTemplate template = new MenuAccessEntityTemplate();
			
			template.setEntityMgtTemplateId(entity.getId());
			template.setViewModel(entity.getViewModel());
			template.setMenuItemId(menuItem.getId());
			if(CommonUtil.isNull(template.getViewModel())){
				template.setViewModel(menuItem.getViewModel());
			}
			template.setEntityMgtTemplate(entity);
			list.add(template);
//			facade.getServiceFactory().getMetaPlatformService().insertMenuAccessEntityTemplate(template);
		}
		facade.getServiceFactory().getMetaPlatformService().deleteMenuAccessEntityTemplate(menuItem.getId(),list);
		super.doUpdateData(arg0, arg1);
	}
}
